package cn.itcast.controller.system;

import cn.itcast.base.BaseController;
import cn.itcast.domain.module.Module;
import cn.itcast.domain.role.Role;
import cn.itcast.service.system.ModuleService;
import cn.itcast.service.system.RoleService;
import cn.itcast.utils.commons.UtilFuns;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping(value = "/system/role")
public class RoleController extends BaseController{
    @Autowired
    private RoleService roleService;
    @Autowired
    private ModuleService moduleService;
//companyId来自登录角色
    @RequestMapping(value = "/list.do",name = "分页查询")
    public String findAllPage(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "4") int pageSize) {
        PageInfo pageInfo = roleService.findAllPage(pageNum, pageSize, companyId);
        request.setAttribute("page",pageInfo);
        return "system/role/role-list";
    }
    @RequestMapping(value = "/toUpdate",name = "角色的编辑页面")
    public String toUpdate(String id) {
        Role role = roleService.findById(id);
        request.setAttribute("role",role);
        return "system/role/role-update";
    }
    @RequestMapping(value = "/delete")
    public String delete(String id) {

        roleService.delete(id);
        //到最新页面展示数据
        return "redirect:/system/role/list.do";
    }
    @RequestMapping(value = "/edit",name = "角色的新增")
    public String edit(Role role) {
        //设置企业标识
        role.setCompanyId(companyId);
        //设置企业名称
        role.setCompanyName(companyName);
        if (UtilFuns.isEmpty(role.getId())) {
            //设置主键
        role.setId(UUID.randomUUID().toString().replaceAll("-",""));
            //保存
            roleService.save(role);
        } else {
            roleService.update(role);
        }

        return "redirect:/system/role/list.do";
    }
    @RequestMapping(value = "/toAdd",name = "角色的新增页面")
    public String toAdd() {
        return "system/role/role-add";
    }


    @RequestMapping(value = "/updateRoleModule",name = "角色添加/修改菜单权限")
    public String updateRoleModule(String roleid, String moduleIds) {
        roleService.updateRoleModule(roleid,moduleIds);
        return "redirect:/system/role/list.do";
    }

    @RequestMapping(value = "/roleModule",name = "角色选权限的页面")
    public String roleModule(String roleid) {
        // 拿着当前的角色id获取角色到页面展示角色名称
        Role role = roleService.findById(roleid);
        request.setAttribute("role",role);
        //到角色选权限的页面
        return "system/role/role-module";
    }

    // [{"id":1,"pId":0,"name":"aaa"}]
    @RequestMapping(value = "/initModuleData",name = "获取ztree的权限数据")
    public @ResponseBody List initModuleData(String roleId) {
        // 0 获取当前角色已具备的菜单权限
        List<Module> roleModule=moduleService.findRoleModule(roleId);
        // 1 获取所有的权限
        List<Module> moduleList = moduleService.findModule();
        // 2 组装符合ztree需要的数据
        List<Map> ztreeList=new ArrayList(); // [{key:value},{key:value}]
        for (Module module : moduleList) {
            HashMap map = new HashMap();
            //设置数据
            map.put("id",module.getId());
            map.put("pId",module.getParentId());
            map.put("name",module.getName());

            // 判断角色的菜单权限是否包含当前循环的菜单权限
            if(roleModule.contains(module)){
                map.put("checked",true);
            }
            // 放入集合
            ztreeList.add(map);
        }
        // 3 将组装的数据返回给页面ajax(json)
        return ztreeList;
    }

}
